<script setup>
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { useUserStore } from '@/stores/user.js'
import {storeToRefs} from "pinia";

const router = useRouter()
const user = storeToRefs(useUserStore());
const username = ref('')
const password = ref('')
console.log(user)
const login = () => {
    //验证

    // 匹配
    let res = ref(user.userList.value.find(item => item.name === username.value && item.password === password.value));
  console.log(res)
    if(!res) {
        alert('用户不存在或者密码错误')
    }else {
        // 匹配成功，登录
        router.push({ path: '/home'})
        user.currentUserInfo.value = res.value
        // 本地存储
        sessionStorage.setItem('isLogin', true)

        // localStorage
    }
}
</script>

<template>
  <div class="login">
        <div class="login_img">
            <img src="http://www.dell-lee.com/imgs/vue3/user.png" alt="">
        </div>
        <div class="login_phone">
            <input type="text" v-model="username" placeholder="请输入用户名">
        </div>
        <div class="login_password">
            <input type="password" v-model="password" placeholder="请输入密码">
        </div>
        <div class="login_btn" @click="login">登录</div>
        <div class="login_link" @click="router.push({ path: '/register'})">立即注册</div>
    </div>
</template>

<style lang="scss" scoped>
@import '@/style/viriables.scss';
.login {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);

    &_img {
        img {
            width: .66rem;
            height: .66rem;
            display: block;
            margin: 0 auto .4rem auto;
        }
    }
    &_phone, &_password {
        margin: 0 .4rem .16rem .4rem;
        line-height: .48rem;
        border: .01rem solid rgba(0, 0, 0, .1);
        border-radius: .06rem;
        padding: 0 .16rem;
        background-color: $dark-bgColor;

        input {
            border: none;
            outline: none;
            line-height: .48rem;
            width: 100%;
            background: none;
            font-size: .16rem;
            color: $content-fontcolor;
        }
    }
    &_btn {
        margin: .32rem .4rem .16rem .4rem;
        line-height: .48rem;
        text-align: center;
        background: $btn-bgColor;
        box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
        border-radius: 0.04rem;
        color: $bgColor;
        font-size: 0.16rem;
        cursor:pointer;
    }
    &_link {
        text-align: center;
        font-size: .14rem;
        color: $content-notice-fontcolor;
    }
}
</style>